package com.olive.controller;

import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.olive.dto.NoticeDTO;
import com.olive.service.impl.MessageSender;

@RestController
public class MessageController {
	
	@Autowired
	private MessageSender messageSender;
	
	@RequestMapping(value = "/send", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> sendMqtt(@RequestParam(value = "msg") String message) {
        System.out.println("生产MQTT消息: " + message);
        NoticeDTO dto = new NoticeDTO();
        dto.setId(new Random().nextInt(100));
        dto.setMessage(message);
        messageSender.publish(dto);
        return new ResponseEntity<>("OK", HttpStatus.OK);
    }
	
}
